package com.zhulanhua.blogs.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhulanhua.blogs.Utils.*;
import com.zhulanhua.blogs.controller.VO.UserInfoVO;
import com.zhulanhua.blogs.dao.UserDao;
import com.zhulanhua.blogs.domain.User;
import com.zhulanhua.blogs.exception.BusinessException;
import com.zhulanhua.blogs.service.ArticleService;
import com.zhulanhua.blogs.service.UserService;
import org.apache.tomcat.util.descriptor.web.ContextHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Random;

@Service
public class UserServiceImpl implements UserService {
    
    @Autowired
    UserDao userDao;

    @Autowired
    MailUtils mailUtils;
    
    @Autowired
    MD5Utils md5Utils;

    @Autowired
    DozerUtils dozerUtils;

    @Override
    public User getDateils() {
        List<User> users = userDao.selectList(null);
        if (users == null){
            throw new BusinessException(Code.BUSINESS_ERR,"用户不存在");
        }
        User user = users.get(0);

        return user;
    }

    @Override
    public boolean update(User user) {
        Map<String,String> token = (Map<String,String>)RequestContextUtils.getHttpServletRequest().getAttribute("token");
        String userId = token.get("userId");
        user.setUserId(Integer.valueOf(userId));
        return userDao.updateById(user) == 1;
    }

    @Override
    public boolean updatePassword(User user, String str) {
        HttpSession session = RequestContextUtils.getHttpServletRequest().getSession();
        String decrypt = (String)session.getAttribute("md5");
        if (decrypt != null){
            if (decrypt.equals(str)){
                QueryWrapper<User> qw = new QueryWrapper<>();
                qw.lambda()
                        .eq(User::getUserName, user.getUserName())
                        .eq(User::getUserMail, user.getUserMail());
                User us = new User();
                us.setPassword(user.getPassword());
                boolean b = userDao.update(us, qw) == 1;
                if (!b){
                    throw new BusinessException(Code.BUSINESS_ERR, "重置密码异常");
                }
                return b;
            }else {
                throw new BusinessException(Code.BUSINESS_ERR, "验证码错误");
            }
        }
        return false;
    }

    @Override
    public String getMD5(User user) {

        QueryWrapper<User> qw = new QueryWrapper<>();
        qw.lambda()
                .eq(User::getUserName, user.getUserName())
                .eq(User::getUserMail, user.getUserMail());

        User user1 = userDao.selectOne(qw);

        if (user1 == null){
            throw new BusinessException(Code.BUSINESS_ERR, "用户名或邮箱有误！");
        }

        String md5 = "";
        Random ran = new Random();
        for (int i = 0;i < 6;i++){
            md5 += ran.nextInt(10);
        }
        try {
            RequestContextUtils.getHttpServletRequest().getSession().setAttribute("md5", md5);
            mailUtils.sendTextMailMessage(user1.getUserMail(),"博客系统", "你正在修改密码，验证码为：" + md5);
        }catch (Exception e){
            throw new BusinessException(Code.BUSINESS_ERR, "验证码发送失败");
        }
        return "";
    }

    @Override
    public UserInfoVO getInfo(int id) {
        QueryWrapper<User> qw = new QueryWrapper<>();
        qw.select("user_name,user_img");
        qw.lambda().eq(User::getUserId, id);
        User user = userDao.selectOne(qw);
        return dozerUtils.convert(user,UserInfoVO.class);
    }

}
